package org.fight.landowner.remoting.protocol;

import org.fight.landowner.common.serialize.SerializableType;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author wangxuehao02
 * @Date 2024/2/3 1:08 AM
 * @Description
 **/
public class RemotingCommandHeader implements Serializable {
    private RemotingCommandHeader() {
    }

    public static RemotingCommandHeader buildRemotingCommandHeaderByTypeCode(byte type) {
        RemotingCommandType remotingCommandType = RemotingCommandType.getByCode(type);

        switch (remotingCommandType) {
            case REQUEST: {
                return buildRequestRemotingCommandHeader();
            }
            case RESPONSE: {
                return buildResponseRemotingCommandHeader();
            }
            default: {
                return null;
            }
        }
    }

    public static RemotingCommandHeader buildRequestRemotingCommandHeader() {
        RemotingCommandHeader header = new RemotingCommandHeader();
        header.type = RemotingCommandType.REQUEST;

        int requestId = requestIdGenerate.incrementAndGet();
        header.requestId = requestId;

        return header;
    }

    public static RemotingCommandHeader buildResponseRemotingCommandHeader() {
        RemotingCommandHeader header = new RemotingCommandHeader();
        header.type = RemotingCommandType.RESPONSE;

        return header;
    }

    /**
     * 类型
     * 请求/响应
     */
    private RemotingCommandType type;

    /**
     * code 仅响应存在 0 代表响应成功，其他代表响应失败
     */
    private short code;

    /**
     * 版本
     */
    private short version;

    /**
     * 语言
     */
    private LanguageType language = LanguageType.JAVA;

    /**
     * 唯一请求 id
     */
    private static final AtomicInteger requestIdGenerate = new AtomicInteger(0);

    private int requestId;

    /**
     * 序列化协议
     */
    private SerializableType serializableProtocol = SerializableType.JSON;

    /**
     * 扩展信息
     */
    private Map<String, String> extendMessage;

    public RemotingCommandType getType() {
        return type;
    }

    protected Map<String, String> getExtendMessage() {
        return extendMessage;
    }

    public LanguageType getLanguage() {
        return language;
    }

    public short getVersion() {
        return version;
    }

    protected void setVersion(short version) {
        this.version = version;
    }

    public short getCode() {
        return code;
    }

    protected void setExtendMessage(Map<String, String> extendMessage) {
        this.extendMessage = extendMessage;
    }

    public void setCode(short code) {
        this.code = code;
    }

    public int getRequestId() {
        return requestId;
    }

    public void setLanguage(LanguageType language) {
        this.language = language;
    }

    public void setRequestId(int requestId) {
        this.requestId = requestId;
    }

    public SerializableType getSerializableProtocol() {
        return serializableProtocol;
    }

    public void setSerializableProtocol(SerializableType serializableProtocol) {
        this.serializableProtocol = serializableProtocol;
    }

    public String addExtend(String key, String val) {
        if (extendMessage == null) {
            extendMessage = new HashMap();
        }

        return extendMessage.put(key, val);
    }

    public String deleteExtend(String key) {
        if (extendMessage == null) {
            return null;
        }

        return extendMessage.remove(key);
    }

    public String getExtend(String key) {
        if (extendMessage == null) {
            return null;
        }

        return extendMessage.get(key);
    }


}


